package org.douxiaoban.client.activity;

import org.douxiaoban.client.R;
import org.douxiaoban.client.api.UserInfoData;
import org.douxiaoban.client.app.App;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.ThreadTool;
import org.douxiaoban.client.widget.PopupSearchBar;
import org.douxiaoban.client.widget.TitleStyle;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.feedback.NotificationType;
import com.feedback.UMFeedbackService;
import com.mobclick.android.MobclickAgent;

/**
 * Created by IntelliJ IDEA.
 * User: huangzhongkun
 * Date: 11-5-22
 * Time: 下午4:12
 */
public class NavigationActivity extends BaseActivity {
    private TitleStyle titleStyle;
    private App _app ;
    private ThreadTool threadTool;
   
    private TextView welcome_tx;

    
//    static{
//		AdManager.init("3169379c50f0428c", "1a8aeac7d0f357d6", 30, false, 0.1);
//	}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        this.setContentView(R.layout.navigation);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.custom_title);
        threadTool = new ThreadTool(this);
        threadTool.startThreadFetchUserInfo(handler);

        titleStyle = new TitleStyle(this);
        titleStyle.initTitleStyle();
        welcome_tx = (TextView)this.findViewById(R.id.welcome_tx);
        welcome_tx.setText(getString(R.string.welcome_hi)+App.username+getString(R.string.welcome_come));
        _app =  (App)getApplicationContext();

        MobclickAgent.setUpdateOnlyWifi(false);
        MobclickAgent.update(this, 1000 * 60 * 60 * 24);
        UMFeedbackService.enableNewReplyNotification(
            	getApplicationContext(), NotificationType.NotificationBar);
        
        this.findViewById(R.id.sao_mao_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                startActivity(new Intent(NavigationActivity.this,CaptureActivity.class));
            }
        });



        this.findViewById(R.id.books_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(NavigationActivity.this,
                        TopActivity.class);
                intent.putExtra(Assistant.KEY_OF_TYPE,Assistant.STATUS_BOOK);
                startActivity(intent);
            }
        });


        this.findViewById(R.id.movie_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent movieIntent = new Intent();
                movieIntent.setClass(NavigationActivity.this,
                        TopActivity.class);
                movieIntent.putExtra(Assistant.KEY_OF_TYPE,Assistant.STATUS_MOVICE);
                startActivity(movieIntent);
            }
        });


        this.findViewById(R.id.music_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                 Intent musicIntent = new Intent();
                musicIntent.setClass(NavigationActivity.this,
                        TopActivity.class);
                musicIntent.putExtra(Assistant.KEY_OF_TYPE,Assistant.STATUS_MUSIC);
                startActivity(musicIntent);
            }
        });


        this.findViewById(R.id.search_lay).setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                _searchbar.showSearchBar();
//                 Intent movieIntent = new Intent();
//                movieIntent.setClass(NavigationActivity.this,
//                        TopActivity.class);
//                startActivity(movieIntent);
            }
        });

        setDefaultKeyMode(DEFAULT_KEYS_SEARCH_LOCAL);
        
//        LinearLayout container =(LinearLayout)findViewById(R.id.AdLinearLayout);
//		container.addView(new AdGroupLayout(this));
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if(msg.what == Assistant.FETCH_USER_DATA_DONE){
                Bundle bundle = msg.getData();
                UserInfoData userInfoData =
                        (UserInfoData)bundle.getSerializable(Assistant.KEY_OF_IMAGE);
                if(userInfoData!=null&& userInfoData.mAvatar!=null){
                    titleStyle.setAvatar(userInfoData.mAvatar);
                }
            }
        }
    };

//
//    @Override
//    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if(keyCode == KeyEvent.KEYCODE_SEARCH){
//            PopupSearchBar popupSearchBar = new PopupSearchBar(NavigationActivity.this);
//            popupSearchBar.showSearchBar();
//        }
//        super.onKeyDown(keyCode,event);
//        return true;
//    }

    @Override
    public boolean onSearchRequested() {
        System.out.println();
        Bundle bundle = new Bundle();

        return super.onSearchRequested();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        _app.mentOptions(item.getItemId(),this);
        return true;
    }
    
    @Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			String title = "您确认退出豆小瓣么？";
			String btnOk = "确定";
			String btnCancel = "取消";
			AlertDialog.Builder ad = new AlertDialog.Builder(this)
					.setTitle(title);
			ad.setPositiveButton(btnOk, new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					try {
						finish();
						// 干掉自己的进程~
						android.os.Process.killProcess(android.os.Process
								.myPid());
					} catch (Throwable e) {
					}
				}
			});
			ad.setNegativeButton(btnCancel, null);
			ad.create().show();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

 }